﻿using FreeOPC.Config;
using FreeOPC.Config.Model;
using FreeOPC.Server.sqlite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FreeOPC.Server.OPCServer
{
    public class OPCStartService
    {
        public static List<OpcDaServer> _daServerList =new List<OpcDaServer>();

        public static void InitOPCServer()
        {
            SerilogServer.WriteLog("FreeOPCSever", new string[] { "服务OPC初始化完成" });
            try
            {
                var opcServerList = SqliteHelper.GetDataTable("select * from opcserver");
                foreach (DataRow opcServer in opcServerList.Rows)
                {
                    var svrMode = new OPCServerModel()
                    {
                        DAProgId = opcServer["DAProgId"].ToString(),
                        AEProgId = opcServer["AEProgId"].ToString(),
                        Id = int.Parse(opcServer["Id"].ToString()),
                        OPCServerIP = opcServer["OPCServerIP"].ToString(),
                        SubsystemTypeId = int.Parse(opcServer["SubsystemTypeId"].ToString()),
                    };
                    var _targetServer = new OpcDaServer(svrMode);
                    _daServerList.Add(_targetServer);
                    _targetServer.Start();

                }
            }
            catch (Exception ex)
            {
                SerilogServer.WriteLog("FreeOPCSever", new string[] { "服务OPC初始化error "+ex.Message+ex.StackTrace });
            }
        }

        /// <summary>
        /// 停止OPC的订阅
        /// </summary>
        /// <param name="opcid">opc ip</param>
        public static void StopServer(string opcip)
        {
            var opcServer = _daServerList.Where(x => x._serverConfig.OPCServerIP == opcip).FirstOrDefault();
            if (opcServer!=null)
            {
                opcServer.Stop();
                _daServerList.Remove(opcServer);
            }
        }

        /// <summary>
        /// 如果不存在了，则直接开启线程
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="OPCServerIP"></param>
        /// <param name="DAProgId"></param>
        /// <param name="AEProgId"></param>
        /// <param name="SubsystemTypeId"></param>
        public static  void StartServer(int Id,string OPCServerIP, string DAProgId, string AEProgId, int SubsystemTypeId)
        {
            var opcServer = _daServerList.Where(x => x._serverConfig.OPCServerIP == OPCServerIP).FirstOrDefault();
            if (opcServer == null)
            {
                var svrMode = new OPCServerModel()
                {
                    DAProgId = DAProgId,
                    AEProgId = AEProgId,
                    Id = Id,
                    OPCServerIP = OPCServerIP,
                    SubsystemTypeId = SubsystemTypeId,
                };
                var _targetServer = new OpcDaServer(svrMode);
                _daServerList.Add(_targetServer);
                _targetServer.Start();
            }  
        }
    }
}
